<?php 
	//header("Content-type: text");
	
	session_start();
	
	require "Twitter/index.php";
	require "Tumblr/blogRead.php";
	require "Facebook/index.php";
	require "Instagram/index.php";
	require "RSS/index.php";
	
// Get PageIdx / PageNum / Search Key
	$xml = simplexml_load_file('php://input');
//	$xml = simplexml_load_file('StreamA.xml');
	//$xml = simplexml_load_string('<Stream Pages="10" PageIndex="1" Category="" Color="Black" Tags="" Location=""><bespoke UserName="asd@eqw.qwe" Channel="Show All" /><facebook ID="" token="" /><twitter key="" secret="" /><tumblr name="" /><instagram token="" /></Stream>');//
	
//	var_dump('php://input');
	
//	$input = file_get_contents('php://input');
// replace & followed by a bunch of letters, numbers
// and underscores and an equal sign with &amp;
//$input = preg_replace('#&(?=[a-z_0-9]+=)#', '&amp;', $input);
//$xml = simplexml_load_file($input);

	foreach( $xml->attributes( ) as $attr)
	{
		$AttrArray[] = $attr;
	}
	
	$nMaxNum = $AttrArray[0];
	$nPageIdx= $AttrArray[1];
	//$nPageIdx = $_POST['XMLidx'];
	$nItemIdx = ($nPageIdx - 1) * $nMaxNum;
	$strCategory = $AttrArray[2];
	$strColor = $AttrArray[3];
	$strTag = $AttrArray[4];
	$nLocation = $AttrArray[5];
	
	//$TwitterAcc = $xml->children();
	foreach( $xml->children() as $child)
	{
		switch ($child->getName())
		{
			case "bespoke":
				foreach( $child->attributes( ) as $attr)
					$Bespoke[] = (string)$attr;
				$Channel = $Bespoke[1];
				break;
			case "facebook":
				foreach( $child->attributes( ) as $attr)
					$Facebook[] = (string)$attr;
				break;
			case "twitter":
				foreach( $child->attributes( ) as $attr)
					$Twitter[] = (string)$attr;
				break;
			case "tumblr":
				foreach( $child->attributes( ) as $attr)
					$Tumblr[] = (string)$attr;
				break;
			case "instagram":
				foreach( $child->attributes( ) as $attr)
					$Instagram[] = (string)$attr;
				break;
			case "googleReader":
				foreach( $child->attributes( ) as $attr)
					$GoogleReader[] = (string)$attr;
				break;
		}
	}

	if( $nItemIdx == 0 )
	{
		session_unset ( );
		
		// Get Each queue = getItem( 0, PageNum )
		if(isset($Bespoke) && strlen($Bespoke[0]) > 0)
			$_SESSION['QBeSpoke'] = GetDataFromSQL( $nItemIdx, $nMaxNum, $strCategory, $strColor, $strTag, $nLocation, $Channel );
		if(isset($Facebook) && strlen($Facebook[0]) > 0)
			$_SESSION['QFacebook'] = GetFacebookMedia( Array('limit'=>$nMaxNum,'Access_id'=>$Facebook[0],'Access_token'=>$Facebook[1],'Reset'=>'true'));
		if(isset($Twitter) && strlen($Twitter[0]) > 0)
			$_SESSION['QTwitter'] = GetTwitterMedia( Array("twitter_count"=>$nMaxNum,'Access_token'=>$Twitter[0],'Access_token_secret'=>$Twitter[1],'Reset'=>'true'));
		if(isset($Tumblr)&& strlen($Tumblr[0]) > 0)	
			$_SESSION['QTumblr'] = GetTumblrMedia( Array('limit'=>$nMaxNum,'uname'=>$Tumblr[0],'Reset'=>'true'));
		if(isset($Instagram)&& strlen($Instagram[0]) > 0)
			$_SESSION['QInstagram'] = GetInstagramMedia( Array('limit'=>$nMaxNum,'Access_Token'=>$Instagram[0],'Reset'=>'true'));
		if(isset($GoogleReader)&& strlen($GoogleReader[0]) > 0)
			$_SESSION['QRSS'] = GetRSSMedia( $GoogleReader );
		// each QEnd = PageNum
		$_SESSION['QEnd'] = array( "BeSpoke"=>$nMaxNum+1, "Facebook"=>$nMaxNum+1, "Twitter"=>$nMaxNum+1, "Tumblr"=>$nMaxNum+1, "Instagram"=>$nMaxNum+1 );
	}
// else 
	else
	{
	// if each QLength < PageNum
		//queue += getItem( QEnd, PageNum )
		//QEnd += PageNum
		if(isset($Bespoke) && strlen($Bespoke[0]) > 0 )
		{
			if( count($_SESSION['QBeSpoke']) < $nMaxNum)
			{
				$_SESSION['QBeSpoke'] = array_merge($_SESSION['QBeSpoke'], GetDataFromSQL( $_SESSION['QEnd']['BeSpoke'], $nMaxNum, $strCategory, $strColor, $strTag, $nLocation, $Channel ));
				$_SESSION['QEnd']['BeSpoke'] += $nMaxNum;
			}
		}
		if( isset($Facebook) && strlen($Facebook[0]) > 0 )
		{ 
			if(count($_SESSION['QFacebook']) < $nMaxNum)
			{
				$_SESSION['QFacebook'] = array_merge($_SESSION['QFacebook'], GetFacebookMedia( Array('limit'=>$nMaxNum,'Access_id'=>$Facebook[0],'Access_token'=>$Facebook[1],'Reset'=>'false')));
				$_SESSION['QEnd']['Facebook'] += $nMaxNum;
			}	
		}	
		if( isset($Twitter) && strlen($Twitter[0]) > 0 )
		{ 
			if(count($_SESSION['QTwitter']) < $nMaxNum/*&& isset($Twitter)*/)
			{
				$_SESSION['QTwitter'] = array_merge($_SESSION['QTwitter'], GetTwitterMedia( Array("twitter_count"=>$nMaxNum,'Access_token'=>$Twitter[0],'Access_token_secret'=>$Twitter[1],'Reset'=>'false')));
				$_SESSION['QEnd']['Twitter'] += $nMaxNum;
			}	
		}
		if( isset($Tumblr) && strlen($Tumblr[0]) > 0 )
		{ 
			if(count($_SESSION['QTumblr']) < $nMaxNum)
			{
				$_SESSION['QTumblr'] = array_merge($_SESSION['QTumblr'], GetTumblrMedia( Array('limit'=>$nMaxNum,'uname'=>$Tumblr[0],'Reset'=>'false')));
				$_SESSION['QEnd']['Tumblr'] += $nMaxNum;
			}	
		}
		if( isset($Instagram) && strlen($Instagram[0]) > 0 )
		{ 
			if(count($_SESSION['QInstagram']) < $nMaxNum)
			{
				$_SESSION['QInstagram'] = array_merge($_SESSION['QInstagram'], GetInstagramMedia( Array('limit'=>$nMaxNum,'Access_Token'=>$Instagram[0],'Reset'=>'false')));
				$_SESSION['QEnd']['Instagram'] += $nMaxNum;
			}	
		}
	}
	
	// Check is Query Empty
	/*
	if( $_SESSION['QEnd']['BeSpoke'] != -1 && count($_SESSION['QBeSpoke']) < $nMaxNum)
		$_SESSION['QEnd']['BeSpoke'] = -1;
	if( $_SESSION['QEnd']['Facebook'] != -1 && count($_SESSION['QFacebook']) < $nMaxNum)
		$_SESSION['QEnd']['Facebook'] = -1;
	if( $_SESSION['QEnd']['Twitter'] != -1 && count($_SESSION['QTwitter']) < $nMaxNum)
		$_SESSION['QEnd']['Twitter'] = -1;
	if( $_SESSION['QEnd']['Tumblr'] != -1 && count($_SESSION['QTumblr']) < $nMaxNum)
		$_SESSION['QEnd']['Tumblr'] = -1;
	if( $_SESSION['QEnd']['Instagram'] != -1 && count($_SESSION['QInstagram']) < $nMaxNum)
		$_SESSION['QEnd']['Instagram'] = -1;
	*/
	// End of Check Empty
	
	echo '<MediaLists>';		
// pop new (n) Item from queues to Output
	for( $i = 0 ; $i < $nMaxNum ; $i++ )
	{
		unset($strKey);
		$Time = 0;
//		Max( $QBeSpoke, $QFacebook, $QTwitter, $QTumblr, $QInstagram);
		if( !empty($_SESSION['QBeSpoke']) )
		{
			$strKey = 'QBeSpoke';
			$Time = $_SESSION['QBeSpoke'][0][0]['lastUpdate'];
		}
		if( !empty($_SESSION['QFacebook']) && $_SESSION['QFacebook'][0][0]['lastUpdate'] > $Time)
		{
			$strKey = 'QFacebook';
			$Time = $_SESSION['QFacebook'][0][0]['lastUpdate'];
		}
		if( !empty($_SESSION['QTwitter']) && $_SESSION['QTwitter'][0][0]['lastUpdate'] > $Time )
		{
			$strKey = 'QTwitter';
			$Time = $_SESSION['QTwitter'][0][0]['lastUpdate'];
		}
		if( !empty($_SESSION['QTumblr']) && $_SESSION['QTumblr'][0][0]['lastUpdate'] > $Time )
		{
			$strKey = 'QTumblr';
			$Time = $_SESSION['QTumblr'][0][0]['lastUpdate'];
		}
		if( !empty($_SESSION['QInstagram']) && $_SESSION['QInstagram'][0][0]['lastUpdate'] > $Time )
		{
			$strKey = 'QInstagram';
			$Time = $_SESSION['QInstagram'][0][0]['lastUpdate'];
		}
		if( !empty($_SESSION['QRSS']) && $_SESSION['QRSS'][0][0]['lastUpdate'] > $Time )
		{
			$strKey = 'QRSS';
			$Time = $_SESSION['QRSS'][0][0]['lastUpdate'];
		}
		
		if( !isset($strKey) )
		{
			break;
		}
//		$Output = array_shift( $QMax );
		$Output = array_shift( $_SESSION[$strKey] );
		OutputXMLFormat( $Output );
	}
	echo '</MediaLists>';	
	
	function GetDataFromSQL( $start, $num, $Category, $Color, $Tag, $Location, $Channel )
	{
		$MediaList = array();
    	//Link to MySql
    	include("mysql_connect.php");

		$strSqlCommand = "SELECT MediaDownloadPath FROM Global_Config WHERE 1";
		$result = mysql_query($strSqlCommand);
		$row = @mysql_fetch_array($result);	
		$ServerPath = $row['MediaDownloadPath'];
 //var_dump($ServerPath);   
		// Search Media Table with 4 Tables' Name
    	
		if(($Channel != "All Bespoke")&&($Channel != "Show All")&&($Channel != "")) {
			
			if($Category != ""){
				$strSqlCommand = "SELECT idMEDIA, MEDIA_TYPE_idMEDIA_TYPE, WebLink, ModifyTime, tbMed.Enable 
						FROM MEDIA tbMed
						LEFT JOIN SPOT_CY_MEDIA tbCatM	ON 	tbMed.idMEDIA = tbCatM.MEDIA_idMEDIA
						LEFT JOIN CATEGORY tbCat		ON 	tbCat.idCATEGORY = tbCatM.CATEGORY_idCATEGORY
						LEFT JOIN COLOR tbCol			ON 	tbCol.idCOLOR = tbMed.COLOR_idCOLOR 
						LEFT JOIN LOCATION tbLoc		ON 	tbMed.LOCATION_idLOCATION = tbLoc.idLOCATION
						LEFT JOIN ZIPCODE tbZip 		ON 	tbLoc.ZIPCODE_idZIPCODE = tbZip.idZIPCODE 
						LEFT JOIN CHANNELS tbCha	    ON 	tbCat.CHANNELS_idCHANNELS = tbCha.idCHANNELS
						WHERE tbMed.Enable = 0  
						AND tbMed.BeMedia_SocialID IS NULL AND tbCha.idCHANNELS = tbCat.CHANNELS_idCHANNELS ";//";
			}
			else {
				$strSqlCommand = "SELECT idMEDIA, MEDIA_TYPE_idMEDIA_TYPE, WebLink, ModifyTime, tbMed.Enable 
						FROM MEDIA tbMed
						LEFT JOIN COLOR tbCol			ON 	tbCol.idCOLOR = tbMed.COLOR_idCOLOR
						LEFT JOIN SPOT_TAG_MEDIA tbTagM	ON 	tbMed.idMEDIA = tbTagM.MEDIA_idMEDIA
						LEFT JOIN TAGS tbTag			ON 	tbTag.idTAGS = tbTagM.TAGS_idTAGS
						LEFT JOIN LOCATION tbLoc		ON 	tbMed.LOCATION_idLOCATION = tbLoc.idLOCATION
						LEFT JOIN ZIPCODE tbZip 		ON 	tbLoc.ZIPCODE_idZIPCODE = tbZip.idZIPCODE
						, CHANNELS tbCha, CATEGORY tbCat, SPOT_CY_MEDIA tbCatM
						WHERE tbMed.Enable = 0  
						AND tbMed.BeMedia_SocialID IS NULL AND tbCha.idCHANNELS = tbCat.CHANNELS_idCHANNELS 
						AND tbMed.idMEDIA = tbCatM.MEDIA_idMEDIA AND tbCat.idCATEGORY = tbCatM.CATEGORY_idCATEGORY ";//";
			}
		}
        else if($Category != ""){
        	$strSqlCommand = "SELECT idMEDIA, MEDIA_TYPE_idMEDIA_TYPE, WebLink, ModifyTime, tbMed.Enable 
						FROM MEDIA tbMed
						LEFT JOIN SPOT_CY_MEDIA tbCatM	ON 	tbMed.idMEDIA = tbCatM.MEDIA_idMEDIA
						LEFT JOIN CATEGORY tbCat		ON 	tbCat.idCATEGORY = tbCatM.CATEGORY_idCATEGORY
						LEFT JOIN COLOR tbCol			ON 	tbCol.idCOLOR = tbMed.COLOR_idCOLOR 
						LEFT JOIN LOCATION tbLoc		ON 	tbMed.LOCATION_idLOCATION = tbLoc.idLOCATION
						LEFT JOIN ZIPCODE tbZip 		ON 	tbLoc.ZIPCODE_idZIPCODE = tbZip.idZIPCODE 
						WHERE tbMed.Enable = 0  
						AND tbMed.BeMedia_SocialID IS NULL ";//";
						
						$Channel = '';
        }
		else{
        	$strSqlCommand = "SELECT idMEDIA, MEDIA_TYPE_idMEDIA_TYPE, WebLink, ModifyTime, tbMed.Enable 
						FROM MEDIA tbMed 
						LEFT JOIN COLOR tbCol			ON 	tbCol.idCOLOR = tbMed.COLOR_idCOLOR
						LEFT JOIN SPOT_TAG_MEDIA tbTagM	ON 	tbMed.idMEDIA = tbTagM.MEDIA_idMEDIA
						LEFT JOIN TAGS tbTag			ON 	tbTag.idTAGS = tbTagM.TAGS_idTAGS
						LEFT JOIN LOCATION tbLoc		ON 	tbMed.LOCATION_idLOCATION = tbLoc.idLOCATION
						LEFT JOIN ZIPCODE tbZip 		ON 	tbLoc.ZIPCODE_idZIPCODE = tbZip.idZIPCODE 
						WHERE tbMed.Enable = 0  
						AND tbMed.BeMedia_SocialID IS NULL ";//";
						
						$Channel = '';
        }
        
		if( strlen($Channel) > 0 )
			$strSqlCommand .= "AND tbCha.Name = '$Channel' ";
		
		if( strlen($Category) > 0 )
			$strSqlCommand .= "AND tbCat.Name = '$Category' ";
	
		if( strlen($Color) > 0 )
			$strSqlCommand .= "AND tbCol.Name = '$Color' ";
	
		if( strlen($Tag) > 0 )
			$strSqlCommand .= "AND tbTag.Name = '$Tag' ";
	
		if( $Location > 0 )
			$strSqlCommand .= "AND tbZip.ZipCode = '$Location' ";
    
		$strSqlCommand .= "ORDER BY ModifyTime DESC LIMIT $start, $num ";
		
		// Get Media Item List
		$result = mysql_query($strSqlCommand);
//echo ($strSqlCommand);	var_dump($result);	
		
		while($rowMedia = @mysql_fetch_array($result))	
		{
			$Media = array();
//var_dump($rowMedia);			
			// Get Comment Count
			$nID = $rowMedia['idMEDIA'];
			$strSqlCommand = "SELECT * FROM COMMENTS 
							  LEFT JOIN USER ON COMMENTS.USER_idUSER = USER.idUSER 
							  WHERE MEDIA_idMEDIA = '$nID'";
			$resultComment = mysql_query($strSqlCommand); 
			$nCommentCount = @mysql_num_rows($resultComment);
			
			// Get Media Type
			$nTypeID = $rowMedia['MEDIA_TYPE_idMEDIA_TYPE'];
			$strSqlCommand = "SELECT Name FROM MEDIA_TYPE WHERE idMEDIA_TYPE = '$nTypeID'";
			$resultMediaType = mysql_query($strSqlCommand);	
			$rowMediaType = @mysql_fetch_array($resultMediaType);
		
			if($nTypeID == 8){
				$RssMediaDetial = array( "Head"=>"Media", "ID"=>$nID, "MediaType"=>$rowMediaType['Name'], "CommentCount"=>$nCommentCount, 
						"WebLink"=>/*$rowMedia['ModifyTime']/*/$rowMedia['WebLink'], "Source"=>"Bespoke", "Author"=>"" ,"lastUpdate"=>strtotime($rowMedia['ModifyTime']));//
				
				array_push( $Media, $RssMediaDetial );
				
				$resultMediaType = mysql_query($strSqlCommand);	
				$rowMediaType = @mysql_fetch_array($resultMediaType);
				
				// Get GoogleReader Data
				$strSqlCommand = "SELECT * FROM TEXT tbText 
								  LEFT JOIN IMAGE tbImage	ON tbText.MEDIA_idMEDIA = tbImage.MEDIA_idMEDIA
								  WHERE tbImage.MEDIA_idMEDIA = '$nID'";
				 
				$resultText = mysql_query($strSqlCommand);	
				while($rowText = mysql_fetch_array($resultText))
				{
					$MediaDetial = array( "Head"=>"beHtml", "ThumbnailImage"=>$rowText['FileName'], "ThumbnailWidth"=>$rowText['Width'], "ThumbnailHeight"=>$rowText['Height'], "Title"=>$rowText['Title'], "Description"=>$rowText['Description']);
					array_push( $Media, $MediaDetial );
				}  
				
				
	        
			}else{
				$MediaDetial = array( "Head"=>"Media", "ID"=>$nID, "MediaType"=>$rowMediaType['Name'], "CommentCount"=>$nCommentCount, 
				"WebLink"=>/*$rowMedia['ModifyTime']/*/$rowMedia['WebLink'], "Source"=>"BeSpoke", "Author"=>"" ,"lastUpdate"=>strtotime($rowMedia['ModifyTime']));//
				array_push( $Media, $MediaDetial );
	
				// Get Text Data
				$strSqlCommand = "SELECT * FROM TEXT WHERE MEDIA_idMEDIA = '$nID'";
				$resultText = mysql_query($strSqlCommand);	
				while($rowText = mysql_fetch_array($resultText))
				{
					$MediaDetial = array( "Head"=>"Text", "ID"=>$rowText['idText'], "Title"=>$rowText['Title'], "Description"=>$rowText['Description']);
					array_push( $Media, $MediaDetial );
				}
			
				// Get Image Data
				$strSqlCommand = "SELECT * FROM IMAGE WHERE MEDIA_idMEDIA = '$nID'";
				$resultImage = mysql_query($strSqlCommand);	
				while($rowImage = mysql_fetch_array($resultImage))
				{
					if( $rowImage['Type'] == "URL" )
					{
						$MediaDetial = array( "Head"=>"Image_Bespoke", "ID"=>$rowImage['idIMAGE'], "Type"=>$rowImage['Type'],
					 							"Width"=>$rowImage['Width'], "Height"=>$rowImage['Height'], "FileName"=>$rowImage['FileName'], 
												"Description"=>"");
					}
					else
					{
						$MediaDetial = array( "Head"=>"Image_Bespoke", "ID"=>$rowImage['idIMAGE'], "Type"=>$rowImage['Type'],
					 							"Width"=>$rowImage['Width'], "Height"=>$rowImage['Height'], "FileName"=>$ServerPath.$nID."/".$rowImage['FileName'], 
												"Description"=>"");
					}
					array_push( $Media, $MediaDetial );
					
					$nImageID = $rowImage['idIMAGE'];
					 
					// Get Note Data
					/*$strSqlCommand = "SELECT * FROM notes WHERE MEDIA_idMEDIA=' $nID'";
					$resultNote = mysql_query($strSqlCommand);
					
					if(mysql_num_rows($resultNote)!=0) {
						$MediaDetial = array( "Head"=>"Image_End", "End"=>">");
						array_push( $Media, $MediaDetial );
					}
					
					while($rowNote = mysql_fetch_array($resultNote))
					{
						$MediaDetial = array( "Head"=>"Note", "ID"=>$rowNote['idNOTES'], "Text"=>$rowNote['NoteText'],
					 							"AxisX"=>$rowNote['AxisX'], "AxisY"=>$rowNote['AxisY']);
												
						array_push( $Media, $MediaDetial );
					}
					
					if(mysql_num_rows($resultNote)!=0)
						$MediaDetial = array( "Head"=>"Image_End", "End"=>"</Image>");
					else
						$MediaDetial = array( "Head"=>"Image_End", "End"=>"/>");
					array_push( $Media, $MediaDetial );*/
					
					//$MediaDetial = array( "Head"=>"Image_End", "End"=>"</Image>");
					//array_push( $Media, $MediaDetial );
				}
			
				// Get Video Data
				$strSqlCommand = "SELECT * FROM Video WHERE MEDIA_idMEDIA = '$nID'";
				$resultVideo = mysql_query($strSqlCommand);	
				while($rowVideo = mysql_fetch_array($resultVideo))
				{
					if( $rowVideo['Type'] == "URL" )
					{
						$MediaDetial = array( "Head"=>"Video", "ID"=>$rowVideo['idVideo'], "Type"=>$rowVideo['Type'],
					 							"FileName"=>$rowVideo['FileName'], "Description"=>"");
					}
					else
					{				
						$MediaDetial = array( "Head"=>"Video", "ID"=>$rowVideo['idVideo'], "Type"=>$rowVideo['Type'],
					 							"FileName"=>$ServerPath.$nID."/".$rowVideo['FileName'], "Description"=>"");
					}
					array_push( $Media, $MediaDetial );
				} 
			} 
			
			while($rowComment = @mysql_fetch_array($resultComment)){
		        		$Mediae = array( "Head"=>"Comment", "ID"=>$rowComment['idCOMMENTS'], "Content"=>$rowComment['Comment'], "User"=>$rowComment['Email']);
						array_push( $Media, $Mediae );
		    }
 			
			$strSqlCommand = "SELECT * FROM notes WHERE MEDIA_idMEDIA=' $nID'";
			$resultNote = mysql_query($strSqlCommand);
			 
			while($rowNote = mysql_fetch_array($resultNote))
			{
						$MediaDetial = array( "Head"=>"Note", "ID"=>$rowNote['idNOTES'], "Text"=>$rowNote['NoteText'],
					 							"AxisX"=>$rowNote['AxisX'], "AxisY"=>$rowNote['AxisY']);
												
						array_push( $Media, $MediaDetial );
			}
					 
			array_push ($MediaList, $Media);
    	}
		
		return $MediaList;
	}
	
	
	function OutputXMLFormat( $OutputData )
	{ 
		foreach( $OutputData as $OutputRow)
		{ 
			switch($OutputRow['Head'])
			{
				case "Media":
					if(isset($OutputRow['BeMediaID']))
					{
						echo '<Media ID="'.$OutputRow['ID'].'" BeMediaID="'.$OutputRow['BeMediaID'].'" MediaType="'.$OutputRow['MediaType'].'" CommentCount="'.$OutputRow['CommentCount'].
						'" WebLink="'.XMLCharEncode($OutputRow['WebLink']).'" Source="'.$OutputRow['Source'].'" Author="'.$OutputRow['Author'].'" lastUpdate="'.date("Y-m-d",($OutputRow['lastUpdate'])).'" >';
					}
					else
					{
						echo '<Media ID="'.$OutputRow['ID'].'" BeMediaID="" MediaType="'.$OutputRow['MediaType'].'" CommentCount="'.$OutputRow['CommentCount'].
						'" WebLink="'.$OutputRow['WebLink'].'" Source="'.$OutputRow['Source'].'" Author="'.$OutputRow['Author'].'" lastUpdate="'.date("Y-m-d",($OutputRow['lastUpdate'])).'" >';
					}
					break;
				case "Text":
						echo '<Text ID="'.$OutputRow['ID'].'" Title="'.XMLCharEncode($OutputRow['Title']).'" Description="'.XMLCharEncode($OutputRow['Description']).'" ></Text>';
					break;
				case "Image":
					echo '<Image ID="'.$OutputRow['ID'].'" Type="'.$OutputRow['Type'].'" FileName="'.$OutputRow['FileName'].'" Description="'.XMLCharEncode($OutputRow['Description']).'" />';
					break;
				case "Image_Bespoke":
					echo '<Image ID="'.$OutputRow['ID'].'" Type="'.$OutputRow['Type'].'" Width="'.$OutputRow['Width'].'" Height="'.$OutputRow['Height'].'" FileName="'.$OutputRow['FileName'].'" Description="'.XMLCharEncode($OutputRow['Description']).'" />';
					break;
				case "Image_End":
					echo $OutputRow['End'];
					break;
				case "Video":
					echo '<Video ID="'.$OutputRow['ID'].'" Type="'.$OutputRow['Type'].'" FileName="'.$OutputRow['FileName'].'" Description="'.XMLCharEncode($OutputRow['Description']).'" />';
					break;
				case "beHtml":
						if(isset($OutputRow['ThumbnailImage'])){
							echo '<beHtml ThumbnailImage="'.$OutputRow['ThumbnailImage'].'" ThumbnailWidth="'.$OutputRow['ThumbnailWidth'].'" ThumbnailHeight="'.$OutputRow['ThumbnailHeight'].'" Title="'.XMLCharEncode($OutputRow['Title']).'" Description="'.XMLCharEncode($OutputRow['Description']).'" />';
						}else{
							echo '<beHtml Title="'.XMLCharEncode($OutputRow['Title']).'" Description="'.XMLCharEncode($OutputRow['Description']).'" />';
						} 
					break;
				case "Note": 
					echo '<Note ID="'.$OutputRow['ID'].'" Text="'.XMLCharEncode($OutputRow['Text']).'" AxisX="'.$OutputRow['AxisX'].'" AxisY="'.$OutputRow['AxisY'].'" ></Note>';
					break;
				case "Comment":
					echo '<Comment ID="'.$OutputRow['ID'].'" Content="'.XMLCharEncode($OutputRow['Content']).'" User="'.XMLCharEncode($OutputRow['User']).'"></Comment>';
					break;
				default:
					echo '<Error Title="Tag Error" />';
			}
		}
		echo '</Media>';
	}
	
	function XMLCharEncode( $Subject )
	{
		$Serach  = array( "<", ">", "&", "'","\"" );
		$Replace = array( "&lt;","&gt;","&amp;","&apos;","&quot;");
		return str_replace($Serach, $Replace, $Subject);
	}
	
	
	
?>